<template>
  <div id="add">
    <el-dialog :title="kind == 'add' ? '添加' : '修改'" :visible.sync="dialogFormVisible" :modal-append-to-body="false" :before-close="closeshow">
      <el-form :model="changeForm" status-icon :rules="rules" ref="changeForm" label-width="50px" class="demo-ruleForm">
        <el-form-item label="卡号" label-width="100px" prop="personNo">
          <el-select v-model="changeForm.personNo" placeholder="请选择卡号" @change="choose(changeForm.personNo)" v-if="kind == 'add'">
            <el-option v-for="item in card" :label="item.personNo" :value="item.personNo" :key="item.subjectNo"></el-option>
          </el-select>
          <el-select v-model="changeForm.personNo" placeholder="请选择卡号" @change="choose(changeForm.personNo)" v-else disabled>
            <el-option v-for="item in card" :label="item.personNo" :value="item.personNo" :key="item.subjectNo"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="姓名" label-width="100px" prop="name">
          <el-input type="text" v-model="changeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="科室" label-width="100px">
          <el-select v-model="changeForm.subjectName" placeholder="请选择科室" @change="guaFei(changeForm.subjectName)">
            <el-option v-for="item in keshi" :label="item.subjectName" :value="item.subjectNo" :key="item.subjectNo"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="挂号费" label-width="100px" prop="registerMoney">
          <el-input type="text" v-model="changeForm.registerMoney"></el-input>
        </el-form-item>
        <el-form-item label="挂号" label-width="100px">
          <el-switch v-model="changeForm.registerYn"></el-switch>
        </el-form-item>
        <el-form-item label="卡种" label-width="100px" prop="identityStatus">
          <el-select v-model="changeForm.identityStatus" placeholder="请选择卡种" v-if="kind == 'add'">
            <el-option label="教师" value="01"></el-option>
            <el-option label="学生" value="02"></el-option>
            <el-option label="普通" value="03"></el-option>
          </el-select>
          <el-select v-model="changeForm.identityStatus" placeholder="请选择卡种" v-else disabled>
            <el-option label="教师" value="01"></el-option>
            <el-option label="学生" value="02"></el-option>
            <el-option label="普通" value="03"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeshow">取 消</el-button>
        <el-button type="primary" @click="what">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { time } from '@/utils/time';
import { mapActions, mapMutations, mapGetters } from 'vuex';
export default {
  props: ['dialogFormVisible', 'changeForm', 'kind'],
  data() {
    var checkNum = (rule, value, callback) => {
      if (!value) {
        callback(new Error('卡号不能为空'));
      } else {
        callback();
      }
    };
    var checkName = (rule, value, callback) => {
      let nameReg = /^([\u4e00-\u9fa5]{2,10}|[a-zA-Z.\s]{2,20})$/;
      if (!value) {
        callback(new Error('姓名不能为空'));
      }
      setTimeout(() => {
        if (nameReg.test(value) == false) {
          callback(new Error('姓名为2-10个中文或英文'));
        } else {
          callback();
        }
      }, 1000);
    };
    var checkSubjectName = (rule, value, callback) => {
      if (!value) {
        callback(new Error('科室不能为空'));
      } else {
        callback();
      }
    };
    var checkMoney = (rule, value, callback) => {
      let checkMoney = /^\w+$/;
      if (!value) {
        callback(new Error('挂号费不能为空'));
      }
      setTimeout(() => {
        if (checkMoney.test(value) == false) {
          callback(new Error('请输入数字'));
        } else {
          callback();
        }
      }, 1000);
    };
    var checkStatus = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请选择卡种'));
      } else {
        callback();
      }
    };
    return {
      keshi: [],
      card: [],

      keName: '',
      formLabelWidth: '120px',
      rules: {
        personNo: [{ validator: checkNum, trigger: 'change' }],
        name: [{ validator: checkName, trigger: 'blur' }],
        subjectName: [{ validator: checkSubjectName, trigger: 'change' }],
        registerMoney: [{ validator: checkMoney, trigger: 'blur' }],
        identityStatus: [{ validator: checkStatus, trigger: 'change' }],
      },
    };
  },

  created() {
    this.getKeshi();
    this.getCard();
    // this.choose()
  },
  methods: {
    ...mapActions(['tongyong']),
    getKeshi() {
      this['tongyong']({
        name: 'CHA_KESHI',
        data: {},
      }).then(resp => {
        if (resp.data.code == 200) {
          this.keshi = resp.data.data;
          console.log(this.keshi);
        } else {
          console.log('查询失败');
        }
      });
    },
    guaFei(id) {
      this.$http({
        url: '/zsdx-outpatient-service/menzhen/subject/select/selectOne/' + id,
        params: {},
        method: 'get',
      }).then(resp => {
        if (resp.data.code == 200) {
          this.changeForm.registerMoney = resp.data.data.registerMoney;
          this.changeForm.subjectName = resp.data.data.subjectName;
        } else {
          console.log(resp);
        }
      });
    },
    getCard() {
      console.log('9999');
      this['tongyong']({
        name: 'CHA_CARD',
        data: {},
      }).then(resp => {
        if (resp.data.code == 200) {
          this.card = resp.data.data;
          console.log(this.card);
        } else {
          console.log('查询失败');
        }
      });
    },
    //选择卡号
    choose(no) {
      console.log(no);
      this.card.map(item => {
        if (item.personNo == no) {
          this.changeForm.identityStatus = item.chargeNo;
          this.changeForm.name = item.personName;
        } else {
          console.log('没有');
        }
      });
    },

    what() {
      if (this.kind == 'add') {
        this.add('changeForm');
      } else if (this.kind == 'xiu') {
        this.xiu('changeForm');
      }
    },
    xiu(obj) {
      this.changeForm.registerYn = this.changeForm.registerYn == true ? 'Y' : 'N';
      let data = this.changeForm;
      this.$refs[obj].validate(valid => {
        if (valid) {
          //id,identityStatus,name,personNo,registerMoney,subjectName,treatDate
          for (var k in data) {
            this.changeForm[k] = data[k];
          }
          // this.changeForm = data
          console.log(this.changeForm);
          this['tongyong']({
            name: 'XIU_REGISTRATION',
            data: this.changeForm,
          }).then(resp => {
            if (resp.data.code == 200) {
              this.$emit('refresh');
              this.$message('修改成功');
            } else {
              this.$message('修改失败');
            }

            this.closeshow();
          });
        } else {
          this.$message('信息不完善');
          return false;
        }
      });
      this.closeshow();
    },
    add(obj) {
      console.log(obj);
      console.log(this.changeForm);
      this.$refs[obj].validate(valid => {
        if (valid) {
          this.changeForm.treatDate = time(new Date());
          console.log(this.changeForm.treatDate);
          this.changeForm.registerYn = this.changeForm.registerYn == true ? 'Y' : 'N';
          console.log(this.changeForm);
          this['tongyong']({
            name: 'ADD_REGISTRATION',
            data: this.changeForm,
          }).then(resp => {
            if (resp.data.code == 200) {
              console.log(resp);
              this.$emit('refresh');
              this.closeshow();
              this.$message('提交成功');
            }
          });
        } else {
          this.$message('信息不完善');
          return false;
        }
      });
    },
    closeshow() {
      // console.log(this.from, this.fromdata);
      this.$emit('close', false);
      //this.data = {};
    },
    change(data) {
      this.$emit('change', data);
    },
  },
};
</script>
<style scoped>
.el-dialog {
  width: 90%;
}
</style>
